# coding: utf-8
#--------------------------------------------<<<<<This script can solve a series of problems>>>>>--------------------------------------------------#
#Date: Always update.
#Author: rosa.w
#Mail: wrx1844@qq.com
#Computer language: Python.3.2.2
#FunctionName=FN.
#------------------------------------------------------------------------------------------------------------------------------------------------------------------#

import maya.cmds as mc
import sys
import maya.OpenMaya as om




#-------------------------------------------------------------------------------------------------------------------------------------
#This Function can query the object's translate and rotate for worldspace or objectspace.
#Note: only support worldspace.
#FN: getSpace(obj)
#Date: 2012/05/10_v1.0

def getSpace(obj):
        '''get one object's worldSpace value'''
        objT = mc.xform(obj,q=1,t=1,ws=1)
        objR = mc.xform(obj,q=1,ro=1,ws=1)
        aboutObjDate = objT + objR
        return aboutObjDate

#-------------------------------------------------------------------------------------------------------------------------------------


#-------------------------------------------------------------------------------------------------------------------------------------
#This Function can make the joint's LRA ok.
#Note: only for "xyz" joint.
#FN: makeJointLRA(*jointList)
#Date: 2012/05/10_v1.0
#           2012/06/28_v1.01


def makeJointLRA(*jointList ):
        '''set joint's lra is ok'''

        for j in jointList:

                mc.joint( j, e=1, oj='xyz', sao='yup', ch=1, zso=1 )
                childJnts = mc.listRelatives( j, ad=1, type='joint' )
                childJnts.reverse()
                for num, c in enumerate( childJnts ):
                        if num == len( childJnts ) - 1:
                                mc.joint( c, e=1, oj='none', zso=1 )
                        else:
                                if mc.getAttr( '%s.jointOrientX'%(c) ) == 180 or -180:
                                        mc.rotate( mc.getAttr(  '%s.jointOrientX'%(c) ), 0, 0, c + '.rotateAxis', r=1 )
                                        mc.joint( c, e=1, zso=1 )
#-------------------------------------------------------------------------------------------------------------------------------------


#-------------------------------------------------------------------------------------------------------------------------------------
#This Function can create skinjoint for spline
#Note: if you want create twist joint ,can use it too.
#FN:
#Date: 2012/06/03_v1.0
#           2012/06/28_v1.01


def createSplineJnt(startjnt,endjnt,num,name,type):
        '''create spline joint with one pathline'''
        #type = ['jnt','ctrl']

        #No.1  create a crv between startjoint and endjoint.

        startJointPoisition = getSpace( startjnt )
        jointList = mc.listRelatives(startjnt,ad=1,type='joint')
        jointList.append( startjnt )
        jointList.reverse()
        pointPos = []

        for j in jointList:
                pointPos.append( mc.xform(j,q=1,ws=1,t=1) )

        crvName= mc.curve( p=pointPos,d=1,n='%s_crv'%startjnt )

        #edit the curve's span cv point and create joint by this point.

        mc.rebuildCurve(crvName,s=(num-1),d=1)
        for i in range(0,int(num) ):
                jntSpace=getSpace( '%s.cv[%d]'%(crvName,i) )
                mc.joint( p=(jntSpace[0],jntSpace[1],jntSpace[2]),n=('%s_0%d_%s' % (name,i+1,type)),radius=0.5 )

        mc.delete(crvName)
        makeJointLRA( '%s_01_%s' % (name,type) )
        #sys.stdout.write('>>>>')


#-------------------------------------------------------------------------------------------------------------------------------------
#This Function can create set driven key with a lot of attribute.
#Note: you can set the Fusion blendshape.
#FN: setDrv(name,*dt,driven)
#Date: 2012/06/03_v1.0(test script)
#        : 2012/06/23_v2.0(Support for different objects and Any number of attributes)



import maya.cmds as mc

def setDrv(name,driven,val,*driver):
        '''super driven key for some attr driven one attr'''

        keys=[]
        for k in range(0,len(driver),2):
                keys.append(driver[k])

        values=[]
        for v in range(1,len(driver),2):
                values.append(driver[v])



        # create the other node and connected
        if len(keys)==1:

                mc.setDrivenKeyframe(driven,cd='%s'%(keys[0]),dv=0,v=0)
                mc.setDrivenKeyframe(driven,cd='%s'%(keys[0]),dv=values[0],v=val)

                sys.stdout.write('>>>>The driver have 1 attribute , setDrivenKey is successed !')

        elif len(keys)==2:

                # create only one mdNode and connect driver's first and secound attribute with setDrivenKey.
                mulNode = mc.createNode('multiplyDivide',n='%sDoubleDrvtoOne_md'%(name))
                mc.setDrivenKeyframe('%s.input1X'%(mulNode),cd='%s'%(keys[0]),dv=0,v=0)
                mc.setDrivenKeyframe('%s.input1X'%(mulNode),cd='%s'%(keys[0]),dv=values[0],v=1)
                mc.setDrivenKeyframe('%s.input2X'%(mulNode),cd='%s'%(keys[1]),dv=0,v=0)
                mc.setDrivenKeyframe('%s.input2X'%(mulNode),cd='%s'%(keys[1]),dv=values[1],v=1)

                # setDrivenKey.
                mc.setDrivenKeyframe(driven,cd='%s.outputX'%(mulNode),dv=0,v=0)
                mc.setDrivenKeyframe(driven,cd='%s.outputX'%(mulNode),dv=1,v=val)

                sys.stdout.write('>>>>The driver have 2 attribute , setDrivenKey is successed !')

        elif int( len(keys) )>2:
                keysNum = len(keys)
                #sys.stdout.write('>>>>The driver have 1 attribute.,setDrivenKey is successed !')

                # create firste mdNode and connect driver's first and secound attribute with setDrivenKey.
                firstNode = mc.createNode('multiplyDivide',n='%sfirstDrvtoOne_md'%(name))
                mc.setDrivenKeyframe('%s.input1X'%(firstNode),cd='%s'%(keys[0]),dv=0,v=0)
                mc.setDrivenKeyframe('%s.input1X'%(firstNode),cd='%s'%(keys[0]),dv=values[0],v=1)
                mc.setDrivenKeyframe('%s.input2X'%(firstNode),cd='%s'%(keys[1]),dv=0,v=0)
                mc.setDrivenKeyframe('%s.input2X'%(firstNode),cd='%s'%(keys[1]),dv=values[1],v=1)

                # create the other node and connected
                node=[]
                for i in range(2,len(keys),1):
                        mdNode = mc.createNode('multiplyDivide',n='%s%sDrvtoOne_md'%(name,i))
                        mc.setDrivenKeyframe('%s.input2X'%(mdNode),cd='%s'%(keys[i]),dv=0,v=0)
                        mc.setDrivenKeyframe('%s.input2X'%(mdNode),cd='%s'%(keys[i]),dv=values[i],v=1)
                        node.append(mdNode)

                mc.connectAttr('%s.outputX'%(firstNode),'%s.input1X'%(node[0]))

                for n in range(len(node)):
                        if n == len(node)-1:
                                pass
                        else:
                                mc.connectAttr('%s.outputX'%(node[n]),'%s.input1X'%(node[n+1]))

                #connect the final Date to the dirven with setDrivenKey.
                last = len(node)
                mc.setDrivenKeyframe(driven,cd='%s.outputX'%(node[last-1]),dv=0,v=0)
                mc.setDrivenKeyframe(driven,cd='%s.outputX'%(node[last-1]),dv=1,v=val)

                sys.stdout.write('>>>>The driver have %s attribute , setDrivenKey is successed !' % (keysNum) )
        else:
                mc.warning('fuck you! man!')

## setDrv('name','被驱动的属性',被驱动的属性值,*driver)  *driver=各种属性和属性值。
## setDrv（'fuck','aa.translateX',50,'bb.translateY',10,'cc.translateY',10,'dd.translateY',10,）
## bb cc dd 的Y轴同时为10的时候，aa的X位移会变成50.
#-------------------------------------------------------------------------------------------------------------------------------------




#-------------------------------------------------------------------------------------------------------------------------------------
#This Function can make object zero
#Note: if you want create control type is joint or you wang create sdk group on objects , you can use this script.
#FN: makeObjZero(sel,type,rotation)
#Date: 2012/07/13_v1.0       only have one type (joint)
#           2012/07/20_v1.1       add a new type(transform) , and you can free to edit have rotating.

import maya.cmds as mc

def makeObjZero(type,suffix,rotation):
        ''' make objects zero and keep spacepoint.'''

        sel = mc.ls(sl=1)

        if len(sel) == 0 :

                mc.error('------------------------->>>You must select one or more object !<<<----------------------------')

        else:

                # this type is joint .

                if type == 'joint':

                        objNum = len(sel)

                        for i in range(0,objNum,1):

                                if  rotation  == 'On':
                                        print 'fuck'
                                        mc.makeIdentity( sel[i] , apply=True , t=1 , r=1 , s=1 , n=0 )
                                        mc.duplicate( sel[i] , name='%s_%s'%(sel[i],suffix) )
                                        childObj = mc.listRelatives( '%s_%s'%(sel[i],suffix) )

                                        if childObj == None:
                                                mc.parent( sel[i] , '%s_%s'%(sel[i],suffix) )
                                        else:
                                                childNum = len(childObj)
                                                mc.delete( '%s_%s|%s' % (sel[i],suffix,childObj[0]) )
                                                mc.parent( sel[i] , '%s_%s'%(sel[i],suffix) )


                                elif rotation == 'Off':

                                        mc.makeIdentity( sel[i] , apply=True , t=1 , r=1 , s=1 , n=0 )
                                        mc.duplicate( sel[i] , name='%s_%s'%(sel[i],suffix) )
                                        mc.makeIdentity( sel[i] , apply=True , t=1 , r=1 , s=1 , n=0 )
                                        mc.setAttr( '%s_%s.rotateX'%(sel[i],suffix) , 0)
                                        mc.setAttr( '%s_%s.rotateY'%(sel[i],suffix) , 0)
                                        mc.setAttr( '%s_%s.rotateZ'%(sel[i],suffix) , 0)
                                        childObj = mc.listRelatives( '%s_%s'%(sel[i],suffix)  )

                                        if childObj == None:
                                                mc.parent( sel[i] , '%s_%s'%(sel[i],suffix) )
                                        else:
                                                childNum = len(childObj)
                                                mc.delete( '%s_%s|%s' % (sel[i],suffix,childObj[0]) )
                                                mc.parent( sel[i] , '%s_%s'%(sel[i],suffix) )

                # type is group ,it is have flag rotateOn / rotateOff ,you can use it for ik controls  con sdk zero and so on.

                elif type == 'group':

                        objNum = len(sel)

                        for i in range(0,objNum,1):

                                grp = mc.createNode('transform' , name = '%s_%s'%(sel[i],suffix) )
                                parentObj = mc.listRelatives( sel[i] , p=True )

                                if rotation == 'On':
                                        #mc.makeIdentity( sel[i] , apply=True , t=0 , r=1 , s=0 , n=0 )
                                        mc.pointConstraint( sel[i] , grp , mo=False)
                                        mc.orientConstraint( sel[i] ,grp , mo=False)
                                        mc.delete( '%s_pointConstraint1'%(grp) )
                                        mc.delete( '%s_orientConstraint1'%(grp) )


                                        if parentObj == None:
                                               mc.parent( sel[i] , grp )
                                        else:
                                               mc.parent( grp , parentObj[0] )
                                               mc.parent( sel[i] , grp )

                                        mc.select(grp)

                                elif rotation == 'Off':

                                        mc.pointConstraint( sel[i] , grp , mo=False )
                                        mc.delete( '%s_pointConstraint1'%(grp) )

                                        if parentObj == None:
                                               mc.parent( sel[i] , grp )
                                        else:

                                               mc.parent( grp , parentObj[0] )
                                               mc.parent( sel[i] , grp )

                                        mc.select(grp)

        #sys.stdout.write('-------------->>> Make object attribute zero is ok !  *Type: %s   *Rotate: %s   *suffix: %s  *ObjNumber: %s'% (type , rotation , suffix , objNum) )

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can add new attribute for objects
#Note: addNewAttr(obj,*attr)
#FN: addNewAttr(obj,*attr)
#Date: 2012/09/09_v1.0

def addNewAttr(obj,dv,minv,maxv,*attr):
        '''add new attribute for select object'''
        mc.select(obj)
        for i in range ( len(attr) ):
                mc.addAttr( longName=attr[i], attributeType='float',defaultValue=dv, minValue=minv, maxValue=maxv, k=True)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can lock and hide attribute for objects
#Note: lockhideAttr(obj,*attr),when the type is 0 ,the attr will be hide ,Contrary the attr is display in channelBox.
#FN: lockhideAttr(obj,*attr)
#Date: 2012/09/25_v1.0

def lockhideAttr(obj,type,*attr):
        '''lock and hide attribute for select object'''

        if type == 0:
                for i in range ( len(attr) ):
                        mc.setAttr( obj+ '.' + attr[i],lock=True,k=False,cb=False)
        elif type == 1:
                for i in range ( len(attr) ):
                        mc.setAttr( obj+ '.' + attr[i],lock=True,k=False,cb=True)

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can zero attribute for objects
#Note: zero obj
#FN: zeroAttr(obj)
#Date: 2012/10/24_v1.0

def zeroAttr(obj):
        mc.setAttr(obj+'.translateX' , 0)
        mc.setAttr(obj+'.translateY' , 0)
        mc.setAttr(obj+'.translateZ' , 0)
        mc.setAttr(obj+'.rotateX' , 0)
        mc.setAttr(obj+'.rotateY' , 0)
        mc.setAttr(obj+'.rotateZ' , 0)

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can quickly create three group with one object, the three group named  'sdk','pos','zero'.
#Note: import RosaCommands as rc
#FN: fastGrp(obj)
#Date: 2012/09/28_v1.0

def fastGrp():
        ''' create three group on the select object'''
        sel = mc.ls(sl=1)
        mc.select(sel[0])
        onegrp = makeObjZero('group','zero','On')
        twogrp = makeObjZero('group','zero','On')
        thirgrp = makeObjZero('group','zero','On')
        mc.makeIdentity( sel[0] , apply=True , t=0 , r=1 , s=0 , n=0 )

        fg = mc.pickWalk(sel[0],d='up')
        sg = mc.pickWalk(fg,d='up')
        tg = mc.pickWalk(sg,d='up')

        mc.rename(fg,'%s_sdk'%(sel[0]))
        mc.rename(sg,'%s_con'%(sel[0]))
        mc.rename(tg,'%s_pos'%(sel[0]))
        mc.select(cl=True)

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can get the distance between tow objects.
#Note: import RosaCommands as rc
#           import math
#FN: getDisValue(st,ed)
#Date: 2012/10/21_v1.0

import math
def getDisValue(st,ed):
        '''can get distance between two objects'''
        stpoint = getSpace(st)
        edpoint = getSpace(ed)
        distance = math.sqrt(   pow( (edpoint[0]-stpoint[0]) , 2 ) + pow( (edpoint[1]-stpoint[1]) , 2 ) + pow( (edpoint[2]-stpoint[2]) , 2 )    )
        return distance

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can create ikhandle about ikhandle and splineikhandle.
#Note:
#FN: createikhandle(st,ed)
#Date: 2012/10/24_v1.0

def createikhandle(st,ed,type,name):

        if type == 'ikhandle':
                iksys = mc.ikHandle( sj=st , ee=ed ,sol='ikRPsolver' , n='%s_ikh'%(name) )
                ikh = iksys[0]
                ikeff = mc.rename(iksys[1] ,'%s_ikheff'%(name) )
                return ikh , ikeff

        elif type == 'ikspline':
                iksys = mc.ikHandle( sj= st , ee= ed , sol='ikSplineSolver', scv=0 , pcv=0 , n= '%s_ikh' % (name)  )
                ikh = iksys[0]
                ikeff = mc.rename( iksys[1] , '%s_ikeff'%(name) )
                ikcurve = mc.rename( iksys[2] , '%s_ikcrv'%(name) )
                return ikh, ikeff,ikcurve

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can get joint list from ikhandle
#Note: must have ikhandle or ikspline
#FN: getBothJnt(ikhandle)
#Date: 2012/10/30_v1.0

def get_ikEffortJnt(ikhandle):
        stjnt = mc.listConnections(ikhandle+'.startJoint')
        #print stjnt
        JntList = mc.listRelatives( stjnt , ad=1 , type = 'joint')
        JntList.append(stjnt[0])
        JntList.reverse()
        #print JntList
        if len(JntList) ==3:
                return JntList[0] , JntList[1] , JntList[2]
        elif len(JntList) == 4:
                return JntList[0] , JntList[1] , JntList[2] , JntList[3]



#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can getParamAtPoint
#Note: for pointOnCurveInfo node's param (Very thanks for ZhaoChunHai and ZhangJian !!! )
#FN: getParam(crv , obj)
#Date: 2012/10/30_v1.0

def node_by_name(name, root = om.MObject.kNullObj):
        dagiter = om.MItDag(om.MItDag.kDepthFirst)
        if not root is om.MObject.kNullObj:
            dagiter.reset(root)
        while not dagiter.isDone():
            pf = om.MFnDagNode(dagiter.currentItem())
            if pf.name() == name:
                return dagiter.currentItem()
            dagiter.next()
        return om.MObject.kNullObj

def getParam(crv , obj):

        curveshape = mc.listRelatives(crv,s=True)
        objspace = getSpace(obj)

        curveObj = node_by_name(curveshape[0])
        curveFn = om.MFnNurbsCurve(curveObj)
        mpoint = om.MPoint( objspace[0],objspace[1],objspace[2],1.0)
        nearestPoint = curveFn.closestPoint(mpoint,None,0.0001,om.MSpace.kTransform )
        u_util = om.MScriptUtil()
        uPtr = u_util.asDoublePtr()
        curveFn.getParamAtPoint(nearestPoint,uPtr,om.MSpace.kTransform)
        u1 = u_util.getDouble(uPtr)
        return u1

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can switch translate and rotate for drv joint.
#Note: like ik fk drv
#FN: drvCtr_swich(ik , fk ,drv)
#Date: 2012/11/06_v1.0
#         :2012/11/22_v1.1( add a attribute 'stretch' )
def drvCtr_swich(prefix , ik , fk , drv , stretch):
        ''' this function can connect three joint translate rotate or scale.only have one switch attribute on the switch group.'''

        ikList = mc.listRelatives(ik , ad=1 , type = 'joint')
        ikList.append(ik)
        ikList.reverse()

        fkList = mc.listRelatives(fk , ad=1 , type = 'joint')
        fkList.append(fk)
        fkList.reverse()

        drvList = mc.listRelatives(drv , ad=1 , type = 'joint')
        drvList.append(drv)
        drvList.reverse()

        switchCmdGrp = mc.createNode('transform' , n=prefix+'_ikfkSwitch_grp')
        addNewAttr(switchCmdGrp,0,0,1,'ikfkswitch')

        if len(ikList)==3:
                aroblender = mc.createNode('blendColors' , n=prefix+'01ro_ikfkswitch_bd')
                broblender = mc.createNode('blendColors' , n=prefix+'02ro_ikfkswitch_bd')
                mc.connectAttr( fkList[0]+'.rotate' , aroblender+'.color2')
                mc.connectAttr( ikList[0]+'.rotate' , aroblender+'.color1')
                mc.connectAttr(switchCmdGrp+'.ikfkswitch' , aroblender+'.blender')
                mc.connectAttr(aroblender+'.output',drvList[0]+'.rotate')

                mc.connectAttr( fkList[1]+'.rotate' , broblender+'.color2')
                mc.connectAttr( ikList[1]+'.rotate' , broblender+'.color1')
                mc.connectAttr(switchCmdGrp+'.ikfkswitch' , broblender+'.blender')
                mc.connectAttr(broblender+'.output',drvList[1]+'.rotate')

                if stretch==0:
                        pass
                else:
                        atrblender = mc.createNode('blendColors' , n=prefix+'01tr_ikfkswitch_bd')
                        btrblender = mc.createNode('blendColors' , n=prefix+'02tr_ikfkswitch_bd')
                        mc.connectAttr( fkList[1]+'.translateX' , atrblender+'.color2R')
                        mc.connectAttr( ikList[1]+'.translateX' , atrblender+'.color1R')
                        mc.connectAttr(switchCmdGrp+'.ikfkswitch' , atrblender+'.blender')
                        mc.connectAttr(atrblender+'.outputR',drvList[1]+'.translateX')

                        mc.connectAttr( fkList[2]+'.translateX' , btrblender+'.color2R')
                        mc.connectAttr( ikList[2]+'.translateX' , btrblender+'.color1R')
                        mc.connectAttr(switchCmdGrp+'.ikfkswitch' , btrblender+'.blender')
                        mc.connectAttr(broblender+'.outputR',drvList[2]+'.translateX')

        elif len(ikList)==4:
                aroblender = mc.createNode('blendColors' , n=prefix+'01ro_ikfkswitch_bd')
                broblender = mc.createNode('blendColors' , n=prefix+'02ro_ikfkswitch_bd')
                croblender = mc.createNode('blendColors' , n=prefix+'03ro_ikfkswitch_bd')
                mc.connectAttr( fkList[0]+'.rotate' , aroblender+'.color2')
                mc.connectAttr( ikList[0]+'.rotate' , aroblender+'.color1')
                mc.connectAttr(switchCmdGrp+'.ikfkswitch' , aroblender+'.blender')
                mc.connectAttr(aroblender+'.output',drvList[0]+'.rotate')

                mc.connectAttr( fkList[1]+'.rotate' , broblender+'.color2')
                mc.connectAttr( ikList[1]+'.rotate' , broblender+'.color1')
                mc.connectAttr(switchCmdGrp+'.ikfkswitch' , broblender+'.blender')
                mc.connectAttr(broblender+'.output',drvList[1]+'.rotate')

                mc.connectAttr( fkList[2]+'.rotate' , croblender+'.color2')
                mc.connectAttr( ikList[2]+'.rotate' , croblender+'.color1')
                mc.connectAttr(switchCmdGrp+'.ikfkswitch' , croblender+'.blender')
                mc.connectAttr(croblender+'.output',drvList[2]+'.rotate')
                if stretch==0:
                        pass
                else:
                        atrblender = mc.createNode('blendColors' , n=prefix+'01tr_ikfkswitch_bd')
                        btrblender = mc.createNode('blendColors' , n=prefix+'02tr_ikfkswitch_bd')
                        ctrblender = mc.createNode('blendColors' , n=prefix+'03tr_ikfkswitch_bd')
                        mc.connectAttr( fkList[1]+'.translateX' , atrblender+'.color2R')
                        mc.connectAttr( ikList[1]+'.translateX' , atrblender+'.color1R')
                        mc.connectAttr(switchCmdGrp+'.ikfkswitch' , atrblender+'.blender')
                        mc.connectAttr(atrblender+'.outputR',drvList[1]+'.translateX')

                        mc.connectAttr( fkList[2]+'.translateX' , btrblender+'.color2R')
                        mc.connectAttr( ikList[2]+'.translateX' , btrblender+'.color1R')
                        mc.connectAttr(switchCmdGrp+'.ikfkswitch' , btrblender+'.blender')
                        mc.connectAttr(btrblender+'.outputR',drvList[2]+'.translateX')

                        mc.connectAttr( fkList[3]+'.translateX' , ctrblender+'.color2R')
                        mc.connectAttr( ikList[3]+'.translateX' , ctrblender+'.color1R')
                        mc.connectAttr(switchCmdGrp+'.ikfkswitch' , ctrblender+'.blender')
                        mc.connectAttr(ctrblender+'.outputR',drvList[3]+'.translateX')

        return switchCmdGrp

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can copy and rename base joint
#Note: like ik fk joint
#FN: copyandRenameJoint(basejoint,name,suffix):
#Date: 2012/10/22_v1.0


def copyandRenameJoint(basejoint,name,suffix):

        '''copy some joint from base joint'''

        newJnt = mc.duplicate(basejoint,rc=True)
        newJntList = mc.listRelatives(newJnt[0],ad=True,type='joint')
        newJntList.append(newJnt[0])
        newJntList.reverse()

        sureJntList=[]
        for i in range ( 0 , len(newJntList) ):
                newjoint = mc.rename( newJntList[i] , '%s0%d_%s'%( name,(i+1),suffix ) )
                sureJntList.append(newjoint)
        return sureJntList

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can Adsorption mesh points
#Note: must have the same two model
#FN: getParam(crv , obj)
#Date: 2012/10/30_v1.0

def adPoint(obj):
        sls = mc.ls( sl=True , fl=True )
        for v in sls:
                list = v.split('.')
                t = mc.xform( '%s.%s'%( obj , list[1] ) , q=1 , ws=1 , t=1)
                mc.xform(v , ws=1 , t=t)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can create rivet by follice
#Note:
#FN: fixedCtrl(inputmesh,*obj):
#Date: 2012/11/22_v1.0

import maya.cmds as mc

def fixedCtrl(inputmesh,*obj):

    mc.createNode('closestPointOnMesh',n='fixedNode')
    meshshape=mc.listRelatives(inputmesh,s=True)[0]
    meshMatrix=str(meshshape)+'.worldMatrix[0]'
    meshout=str(meshshape)+'.outMesh'
    follicleAll=[]

    if mc.objExists('follicle_grp'):
        pass
    else:
        mc.group(em=True,n='follicle_grp')

    for i in obj:
        folliclename=str(i)+'_follicleshape'
        mc.createNode('follicle',n=folliclename)
        follicleparent=mc.listRelatives(folliclename,p=True)[0]
        follicleparentname=str(i)+'_follicle'
        mc.rename(follicleparent,follicleparentname)

        mc.connectAttr(meshMatrix,str(folliclename)+'.inputWorldMatrix')
        mc.connectAttr(meshout,str(folliclename)+'.inputMesh')

        matrixconnect=mc.listConnections('fixedNode.inMesh')
        if matrixconnect==None:
            mc.connectAttr(meshout,'fixedNode.inMesh')
        mc.connectAttr(str(i)+'.translate','fixedNode.inPosition')

        mc.connectAttr(str('fixedNode')+'.parameterU',str(folliclename)+'.parameterU')
        mc.connectAttr(str('fixedNode')+'.parameterV',str(folliclename)+'.parameterV')

        mc.connectAttr(str(folliclename)+'.outTranslate',str(follicleparentname)+'.translate')
        mc.connectAttr(str(folliclename)+'.outRotate',str(follicleparentname)+'.rotate')

        locparameterU=mc.getAttr(str(follicleparentname)+'.parameterU')
        locparameterV=mc.getAttr(str(follicleparentname)+'.parameterV')

        mc.disconnectAttr('fixedNode.parameterU',str(follicleparentname)+'.parameterU')
        mc.disconnectAttr('fixedNode.parameterV',str(follicleparentname)+'.parameterV')

        mc.setAttr('fixedNode.parameterU',locparameterU)
        mc.setAttr('fixedNode.parameterV',locparameterV)

        mc.parentConstraint(follicleparentname,i,maintainOffset=True)
        mc.disconnectAttr(str(i)+'.translate','fixedNode.inPosition')
        follicleAll.append(follicleparentname)

    mc.parent(follicleAll,'follicle_grp')
    mc.delete('fixedNode')

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

